import $ from 'jQuery';
(function () {
  /**
   * 删除账户
   * @param params
   * @private
   */
  var _deleteAccountManagement = function (params) {
    IOT.confirm('你确定要删除吗？', () => {
      IOT.showOverlay('保存中，请稍等...');
      IOT.getServerData(URI.SYSTEM.ACCOUNT_MANAGEMENT.DELETE, params, function (ret) {
        IOT.hideOverlay();
        if (ret && ret.code === 200) {
          IOT.tips('删除成功！', 'success');
          M.Table.refresh.all();
          BootstrapDialog.closeAll();
        } else {
          IOT.tips(ret.msg || '操作失败，请重试！', 'error');
        }
      });
      return false;
    }, null, 'warn');
  };
  let $accountBox = null;
  let $vue = new Vue({
    el: '.main-hook',
    data: {
      leaseType: [{id: 1, name: '短租'}, {id: 2, name: '长租'}],
      receiveStatus: [{id: 1, name: '部分收货'}, {id: 2, name: '全部收货'},{id: 3, name: '订单取消'}],
      orderStatus: [{id: 0, name: '未审核'}, {id: 1, name: '已审核'}, {id: 2, name: '已完结'}, {id: 3, name: '已废弃'}],
      paymentFlag: [{id: 1, name: '未打款'}, {id: 2, name: '已打款至第三方银行'}, {id: 3, name: '已收款'}],
      boxs: {
        box1: {show: true, hook: '.box1-hook', lock: true, url: '', params: {}},
        box2: {show: false, hook: '.box2-hook', url: '', params: {}},
        box3: {show: false, hook: '.box3-hook', url: '', params: {}},
        box4: {show: false, hook: '.box4-hook', url: '', params: {}}
      },
      roleList: []
    },
    created: function () {
      M.Request.getRoleList(true, (roleList, ret) => {
        this.roleList = roleList;
      });
    },
    methods: {
      emitPage: function () {
        M.Page.load(this.boxs);
      },
      // 上一页
      prePage: function () {
        M.Page.prePage(this.boxs);
      },
      searchKey: function () {
        $('.search-hook').trigger('click');
      },
      // 触发刷新页(当前显示的页面)
      refreshPage: function () {
        M.Page.refreshPage(this.boxs);
      },
      // 新增
      createAccountManagement: function () {
        M.Page.emit(URI.SYSTEM.ACCOUNT_MANAGEMENT.CREATE.PAGE);
      },
      // 重置密码
      _resetAccountManagement: function (params){
        layer.open({
          content:'您确定要重置密码吗？'
          ,btn: ['确定', '取消']
          ,title:'重置密码'
          ,area: ['200px', '200px']
          ,shade: [0.3, '#999']
          ,yes: function(index, layero){
            //确定的回调,这里点击确定之后弹出框不会关闭，这时候需要手动关闭。

            layer.close(index); //如果设定了yes回调，需进行手工关闭
          },btn2: function(index, layero){
          }
          ,cancel: function(){
          }
        })
      },
      exportOrder: function () { //下载
        // var staticServer = $('#staticServer').val();
        IOT.showOverlay('正在导出，请稍等...');
        IOT.getServerData(URI.SYSTEM.ACCOUNT_MANAGEMENT.EXPORT, {}, (ret) => {
          IOT.tips(ret.msg || '操作失败，请重试！', 'error', 1000, null);
          IOT.hideOverlay();
          if (ret && ret.code === 200) {
            window.location.href = ret.data.substr(1);
            // console.log(window.location.href);
            IOT.tips('导出完成！', 'success');
          } else {
            IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
          }
        });
        return false;
      }
    },
    mounted: function () {
      let thiz = this;
      this.$nextTick(() => {
        let tableHookName = '.account-management-table-hook';
        $accountBox = $('.account-management-hook');
        var $form = $accountBox.find('.form-hook');
        modal.initDate("startdate","enddate");
        // 查询
        $form.off('submit').on('submit', function () {
          $(tableHookName).bootstrapTable('onCustomSearch');
          return false;
        });
        var $tableHook = $accountBox.find(tableHookName).bootstrapTable({
          scrollbar: 'account-management-table-body',
          striped: true, // 设置为 true 会有隔行变色效果
          pagination: true, // true 显示分页
          paginationDetail: true, // 分页详情
          sidePagination: 'server', // 服务器端
          method: 'post',
          url: URI.SYSTEM.ACCOUNT_MANAGEMENT.LIST,
          debug: false,
          cache: false,
          pageNumber: 1,
          pageSize: 20,
          pageList: [10, 20, 50, 100],
          // fixedColumns: true,
          customButton: '',
          customQueryParams: function (params) {
            var formParams = $form.serializeJson();
            var queryParams = $.extend({}, params, formParams);
            //console.log(queryParams);
            return queryParams;
          },
          onLoadSuccess: function (data) {
            //console.log(JSON.stringify(data));
          },
          onLoadError: function (status, xhr) {
          },
          onCheckAll: function (rows) {

          },
          columns: [
            {
              field: '', checkbox: true, align: 'center', fixedLeft: true, width: 60,
              events: {
                'change .checkbox-hook': function (e, value, row, index) {
                  console.log('复选框: ' + value);
                }
              }
            },
            {
              field: 'userName', title: '账户编号', align: 'center', fixedLeft: true, tips: true
            },
            {
              field: 'mobile', title: '手机号码', align: 'center', fixedLeft: true, tips: true
            },
            {
              field: 'gender', title: '用户姓名', align: 'center', fixedLeft: true, tips: true
            },
            {
              field: 'gender', title: '所属组织', align: 'center', fixedLeft: true, tips: true
            },
            {
              field: 'operate', title: '操作', align: 'center', fixedLeft: true,width: 180,
              events: {
                'click .update-account': function (e, value, rowData, index) {
                  M.Page.emit(URI.SYSTEM.ACCOUNT_MANAGEMENT.UPDATE.PAGE);
                  IOT.setSessionStore(URI.SYSTEM.ACCOUNT_MANAGEMENT.UPDATE.PAGE, JSON.stringify(rowData));
                  return false;
                },
                'click .delete-account': function (e, value, row, index) {
                  _deleteAccountManagement(row);
                  return false;
                }
              },
              formatter: function (value, row, index) {
                let html = [];
                if (M.Authority.checkAuthority('account-modify')) {
                  html.push('<a class="update-account operate">修改</a>');
                }
                if (M.Authority.checkAuthority('account-delete')) {
                  html.push('<a class="delete-account operate">删除</a>');
                }
                return html.join('');
              }
            }
          ]
        });
      });
    }
  });
})();